package com.quanyan.user.web;

import com.alibaba.fastjson.JSONObject;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.user.model.vo.response.RespReceiveDetail;
import com.quanyan.user.request.ReqReceiveEggShell;
import com.quanyan.user.service.ReceiveDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by jian.zhou on 2016/6/30.
 * 领取任务明细
 */
@RestController
@RequestMapping("/api/user/receiveDetail")
public class ReceiveDetailController {

    private static final Logger logger = LoggerFactory.getLogger(ReceiveDetailController.class);

    @Autowired
    private ReceiveDetailService receiveDetailService;

    /**
     * 获取鸟蛋明细
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public APIResponse list( @RequestBody Map map,HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        logger.info("开始获取领取蛋壳明细 uid: {}",uid);
        try {
            List<RespReceiveDetail> respReceiveDetails = receiveDetailService.queryReceiveDetail(uid);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("respReceiveDetailList",respReceiveDetails);
            return APIResponse.returnSuccess(jsonObject);
        } catch (Exception e) {
            logger.info("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }


    /**
     * 领取蛋壳
     * @return
     */
    @RequestMapping(value = "/addTemp",method = RequestMethod.POST)
    public APIResponse addTemp( @RequestBody @Valid ReqReceiveEggShell reqReceiveEggShell,BindingResult bindingResult,HttpServletRequest request){
        if (bindingResult.hasErrors())
             return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);

        int uid = (int)request.getAttribute("uid");
        try {
            reqReceiveEggShell.setUid(uid);
            receiveDetailService.addReceiveDetail(reqReceiveEggShell);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }
    }


    /**
     * 领取蛋壳
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public APIResponse add( @RequestBody @Valid ReqReceiveEggShell reqReceiveEggShell,BindingResult bindingResult,HttpServletRequest request){
        if (bindingResult.hasErrors())
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        int uid = (int)request.getAttribute("uid");
        try {
            reqReceiveEggShell.setUid(uid);
            receiveDetailService.addReceiveDetail(reqReceiveEggShell);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }
    }
}
